//
//  LHVistorView.swift
//  sinaWeibo
//
//  Created by LiHong on 15/10/26.
//  Copyright © 2015年 LiHong. All rights reserved.
//

import UIKit
protocol LHVistorViewDelegate: NSObjectProtocol{
    /// 点击注册
    func vistorViewWillRegister()
  //      print("\(__FUNCTION__)")
    /// 点击登录
    func vistorViewWillLogin()
 //       print("\(__FUNCTION__)")
    
    

}

class LHVistorView: UIView {
    // 属性
    weak var vistorViewDelegate: LHVistorViewDelegate?
    // MARK: - 点击触发事件
    /// 点击注册触发事件
    func vistorViewWillRegister(){
        print("\(__FUNCTION__)")
        //？前面的变量有值才执行后面的代码，没有值就什么都不做
        vistorViewDelegate?.vistorViewWillRegister()
    }
    /// 点击登录触发事件
    func vistorViewWillLogin(){
   //     print("\(__FUNCTION__)")
        vistorViewDelegate?.vistorViewWillLogin()
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func prepareUI() {
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(iconView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(textLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        
        /// 轮盘
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        /// 小房子
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        /// 消息文字
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        /// 登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        /// 注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        /// 遮盖层
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }
    /**
     设置信息
     
     - parameter imageName: 图片名称
     - parameter messagge:  显示内容
     */
    func setupInfo(imageName: String, messagge: String){
        textLabel.text = messagge
        iconView.image = UIImage(named: imageName)
        houseView.hidden = true
        sendSubviewToBack(coverView)
    }
    /// 旋转iconView
    func startRotationAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        // 设置参数
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        // 完成的时候不移除
        animation.removedOnCompletion = false
        
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
    }
    /// 停止动画
    func pauseAnimation() {
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        iconView.layer.speed = 0;
        iconView.layer.timeOffset = pauseTime
    }
    /// 继续动画
    func resumeAnimation() {
        let pauseTime = iconView.layer.timeOffset
        iconView.layer.speed = 1
        iconView.layer.timeOffset = 0
        iconView.layer.beginTime = 0
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        iconView.layer.beginTime = timeSincePause
    }
    
    /// 轮盘
    private lazy var iconView: UIImageView = {
        let iconView = UIImageView()
        iconView.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        iconView.sizeToFit()
        return iconView
    }()
    
    /// 小房子
    private lazy var houseView: UIImageView = {
        let houseView = UIImageView()
        houseView.image = UIImage(named: "visitordiscover_feed_image_house")
        houseView.sizeToFit()
        return houseView
    }()
    /// 消息文字
    private lazy var textLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.text = "关注一些人，回这里看看有什么惊喜"
        textLabel.textColor = UIColor.lightGrayColor()
//        textLabel.font = UIFont.systemFontSize(14)
        textLabel.sizeToFit()
        textLabel.numberOfLines = 0
        
        return textLabel
    }()
    /// 登录按钮
    private lazy var loginBtn: UIButton = {
        let loginBtn = UIButton()
        loginBtn.setTitle("登录", forState: UIControlState.Normal)
        loginBtn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        loginBtn.sizeToFit()
        loginBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        loginBtn.addTarget(self, action: "vistorViewWillLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginBtn
    }()
    /// 注册按钮
    private lazy var registerBtn: UIButton = {
        let registerBtn = UIButton()
        registerBtn.setTitle("注册", forState: UIControlState.Normal)
        registerBtn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerBtn.sizeToFit()
        registerBtn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        registerBtn.addTarget(self, action: "vistorViewWillRegister", forControlEvents: UIControlEvents.TouchUpInside)
        return registerBtn
    }()
    
    /// 遮盖层
    private lazy var coverView: UIImageView = {
        let coverView = UIImageView()
        coverView.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return coverView
    }()
    
}
